package com.risen.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.risen.config.GenConstant;
import com.risen.entity.Attribute;
import com.risen.entity.AutoGen;
import com.risen.entity.Entity;
import com.risen.entity.EntityInfo;
import com.risen.entity.HtmlGen;
import com.risen.entity.JSGen;
import com.risen.entity.JavaCodeGen;
import com.risen.entity.MavenProjectTarget;
import com.risen.entity.MdGen;
import com.risen.entity.WebTarget;
import com.risen.service.CodeGenService;
import com.risen.utils.Page;
import com.risen.utils.R;
import com.risen.utils.RRException;

/**
 * 代码生成器
 * 
 */
@Controller
@RequestMapping("/code/gen")
public class CodeGenController {
	@Autowired
	private CodeGenService codeGenService;
	@Autowired
	private GenConstant genConstant;
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params,Integer page,Integer limit,String key){
		Page<Entity> list = codeGenService.getAllEntityInfo(page,limit,key);
		return R.ok().put("data", list);
	}
	
	@ResponseBody
	@RequestMapping("/refresh")
	public R refresh(){
		codeGenService.refresh();
		return R.ok();
	}
	
	@RequestMapping("/detail")
	public String detail(String tableName,Model model){
		Entity entity = codeGenService.getEntityInfo(tableName);
		model.addAttribute("entity", entity);
		model.addAttribute("gen", genConstant);
		return "detail";
	}
	
	@RequestMapping("/gen")
	@ResponseBody
	public R gen(EntityInfo data,String workSpace) {
		
		Entity  entity = getEntitiyInfo(data);
		String urlBase = data.getUrlBase();
		String basePackage = data.getBasePackage();
		
		if(StringUtils.hasText(data.getEntity().getSystem())) {
			basePackage = basePackage + "." + data.getEntity().getSystem();
		}
		
		MavenProjectTarget target = new MavenProjectTarget(entity, basePackage);
		//生成到path目录下，按照maven工程解构生成
		File file = new File(workSpace);
		if(!file.exists()) {
		    throw new RRException("路径不存在 "+workSpace);
		}
		target.setTargetPath(workSpace);
		target.setUrlBase(urlBase);

		JSGen jsGen = new JSGen();
		jsGen.make(target, entity);

		HtmlGen htmlGen = new HtmlGen();
		htmlGen.make(target, entity);
		String  preffix =  urlBase.replace('/', '.');
		String basicFunctionCode = preffix+":"+entity.getCode();
		JavaCodeGen javaGen = new JavaCodeGen(basePackage, entity,basicFunctionCode);
		javaGen.make(target, entity);

		MdGen mdGen = new MdGen();
		mdGen.make(target, entity);
		
		return R.ok();
	}
	
	@RequestMapping("/html")
	@ResponseBody
	public R html(EntityInfo data) {
		Entity entity = getEntitiyInfo(data);
		String urlBase = data.getUrlBase();
		String basePackage = data.getBasePackage();
		if(StringUtils.hasText(data.getEntity().getSystem())) {
			basePackage = basePackage + "." + data.getEntity().getSystem();
		}
		WebTarget webTarget = new WebTarget(entity, basePackage);
		webTarget.setUrlBase(urlBase);
		HtmlGen htmlGen = new HtmlGen();
		htmlGen.make(webTarget, entity);
		Map<String, String> content = new HashMap<String, String>();
		for (Entry<Object, String> entry : webTarget.map.entrySet()) {
			AutoGen gen = (AutoGen) entry.getKey();
			String code = entry.getValue();
			content.put(gen.getName(), code);
		}
		return R.ok().put("content", content);
	}
	
	@RequestMapping("/js")
	@ResponseBody
	public R js(EntityInfo data) {
		Entity entity = getEntitiyInfo(data);
		String urlBase = data.getUrlBase();
		String basePackage = data.getBasePackage();
		if(StringUtils.hasText(data.getEntity().getSystem())) {
			basePackage = basePackage + "." + data.getEntity().getSystem();
		}
		WebTarget webTarget = new WebTarget(entity, basePackage);
		webTarget.setUrlBase(urlBase);
		JSGen jsGen = new JSGen();
		jsGen.make(webTarget, entity);
		Map<String, String> content = new HashMap<String, String>();
		for (Entry<Object, String> entry : webTarget.map.entrySet()) {
			AutoGen gen = (AutoGen) entry.getKey();
			String code = entry.getValue();
			content.put(gen.getName(), code);
		}
		return R.ok().put("content", content);
	}
	
	@RequestMapping("/java")
	@ResponseBody
	public R java(EntityInfo data) {
		Entity entity = getEntitiyInfo(data);
		String urlBase = data.getUrlBase();
		String basePackage = data.getBasePackage();
		if(StringUtils.hasText(data.getEntity().getSystem())) {
			basePackage = basePackage + "." + data.getEntity().getSystem();
		}
		WebTarget webTarget = new WebTarget(entity, basePackage);
		webTarget.setUrlBase(urlBase);
		
		String  preffix =  urlBase.replace('/', '.');
		String basicFunctionCode = preffix+"."+entity.getCode();
		JavaCodeGen javaGen = new JavaCodeGen(basePackage,entity,basicFunctionCode);
		javaGen.make(webTarget, entity);
		Map<String, String> content = new HashMap<String, String>();
		for (Entry<Object, String> entry : webTarget.map.entrySet()) {
			AutoGen gen = (AutoGen) entry.getKey();
			String code = entry.getValue();
			content.put(gen.getName(), code);
		}
		return R.ok().put("content", content);
	}
	
	@RequestMapping("/sql")
	@ResponseBody
	public R sql(EntityInfo data) {
		Entity entity = getEntitiyInfo(data);
		String urlBase = data.getUrlBase();
		String basePackage = data.getBasePackage();
		if(StringUtils.hasText(data.getEntity().getSystem())) {
			basePackage = basePackage + "." + data.getEntity().getSystem();
		}
		WebTarget webTarget = new WebTarget(entity, basePackage);
		webTarget.setUrlBase(urlBase);
		
		MdGen mdGen = new MdGen();
		mdGen.make(webTarget, entity);
		Map<String, String> content = new HashMap<String, String>();
		for (Entry<Object, String> entry : webTarget.map.entrySet()) {
			AutoGen gen = (AutoGen) entry.getKey();
			String code = entry.getValue();
			content.put(gen.getName(), code);
		}
		return R.ok().put("content", content);
	}
	
	private Entity getEntitiyInfo(EntityInfo data) {
		Entity info = data.getEntity();
		Entity entity = codeGenService.getEntityInfo(info.getTableName());
		entity.setCode(info.getCode());
		entity.setDisplayName(info.getDisplayName());
		entity.setSystem(info.getSystem());
		entity.setAttachment(data.getEntity().isAttachment());
		entity.setIncludeExcel(data.getEntity().isIncludeExcel());
		entity.setAutoAddFunction(info.isAutoAddFunction());
		entity.setAutoAddMenu(info.isAutoAddFunction());
		for (int i = 0; i < entity.getList().size(); i++) {
		    Attribute attr = entity.getList().get(i);
		    attr.setDisplayName(info.getList().get(i).getDisplayName());
		    attr.setShowInQuery(info.getList().get(i).isShowInQuery());
		    attr.setDictType(info.getList().get(i).getDictType());
		    attr.setVerifyList(info.getList().get(i).getVerifyList());
		    if(attr.getName().equals(data.getNameAttr())) {
		        entity.setNameAttribute(attr);
		    }
		}
		
		if (StringUtils.isEmpty(entity.getCode()) || StringUtils.isEmpty(entity.getSystem())) {
			throw new RRException("code,system不能为空");
		}
		
		
		return entity;
	}
	
}
